// pages/home/search/search.js
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        secondTab: 0,
        searchValue: '',
        billList: [], // 发票
        receiveList: [], // 到款
        currentPage: 1, // 当前页码
        pageSize: 10, // 每页显示数据
        loading: true,
        remind: '下拉加载更多',
        nodata: false, //是否没有数据
        isSearch: true
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    getData() {
        if (this.data.active == 0) {
            this.getBillList()
        } else {
            this.getReceiveList()
        }
    },
    // 获取发票
    getBillList() {
        let that = this;
        let flowStatus = 2;
        if (that.data.secondTab == 0) {
            flowStatus = 2
        } else if (that.data.secondTab == 1) {
            flowStatus = 4
        } else if (that.data.secondTab == 2) {
            flowStatus = 3
        }
        api.queryInvoice({
            data: {
                flowStatus: flowStatus,
                searchStr: that.data.searchValue ? that.data.searchValue : '',
                pageNum: that.data.currentPage,
                pageSize: that.data.pageSize
            },
            success: function (res) {
                console.log(res)
                if (res.data.code == 200) {
                    if (res.data.data.list.length > 0) {
                        for (let i = 0; i < res.data.data.list.length; i++) {
                            let now = new Date(res.data.data.list[i].receiveDate);
                            res.data.data.list[i].receiveDate = util.formatTime(now).split(' ')[0];
                            res.data.data.list[i].money = util.formatFigure(res.data.data.list[i].money);
                        }
                        that.data.totalPage = res.data.data.totalPage
                        that.setData({
                            billList: that.data.billList.concat(res.data.data.list),
                            loading: false,
                            nodata: false,
                            remind: res.data.data.totalPage > res.data.data.pageNum ? '下拉加载更多' : '已经到底啦～'
                        })
                    } else {
                        that.setData({
                            nodata: true
                        })
                    }

                }
            }
        })
    },
    // 获取到款
    getReceiveList() {
        let that = this;
        api.queryReceive({
            data: {
                searchStr: that.data.searchValue ? that.data.searchValue : '',
                pageNum: that.data.currentPage,
                pageSize: that.data.pageSize
            },
            success: function (res) {
                console.log(res)
                if (res.data.code == 200) {
                    if (res.data.data.list.length > 0) {
                        for (let i = 0; i < res.data.data.list.length; i++) {
                            let now = new Date(res.data.data.list[i].receiveDate);
                            res.data.data.list[i].date = util.formatTime(now).split(' ')[0];
                            res.data.data.list[i].money = util.formatFigure(res.data.data.list[i].money);
                            res.data.data.list[i].cusBankAccount = res.data.data.list[i].cusBankAccount.substring(res.data.data.list[i].cusBankAccount.length - 4, res.data.data.list[i].cusBankAccount.length);
                        }
                        that.data.totalPage = res.data.data.totalPage
                        that.setData({
                            receiveList: that.data.receiveList.concat(res.data.data.list),
                            loading: false,
                            nodata: false,
                            remind: res.data.data.totalPage > res.data.data.pageNum ? '下拉加载更多' : '已经到底啦～'
                        })
                    } else {
                        that.setData({
                            nodata: true
                        })
                    }

                }
            }
        })
    },

    // 清空数据并重新加载数据
    clearData() {
        this.setData({
            billList: [],
            receiveList:[],
            currentPage: 1,
            loading: true,
            nodata:false
        })
        this.getData()
    },
    // 搜索
    onSearch(event) {
        console.log(event)
        this.setData({
            searchValue: event.detail && event.detail.value ? event.detail.value : event.detail,
        })
        if(event.detail==undefined){
            this.setData({
                searchValue:'',
                billList: [],
                receiveList:[],
                currentPage: 1,
                loading: true,
                nodata:false
            })
        }else{
            this.clearData()
        }
    },
    onChange(event) {
        this.setData({
            active: event.detail.index,
            secondTab: 0,
        })
        this.clearData()
    },
    secondChange(event) {
        this.setData({
            secondTab: event.detail.index
        })
        if(this.data.active == 0){
            this.clearData()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.totalPage > this.data.currentPage) {
            ++this.data.currentPage;
            this.setData({
                loading: true
            })
            this.getData()
        }
    }
})